<?php
/**
 * Created by PhpStorm.
 * User: dingjinlai
 * Date: 2019/7/2
 * Time: 下午7:56
 */

class Custom_purchase_model extends CI_Model
{

    public $limit_rows = 15;

    public function _where($param = array())
    {

        if ($param['custom_state']) {
            $this->db->where_in('cp.custom_state', $param['custom_state']);
        } else {
            if ($param['step'] == 1) {
                $this->db->where('cp.custom_state', 1);
            } elseif ($param['step'] == 2) {
                $this->db->where_in('cp.custom_state', array(2,3,5,6));
            } elseif ($param['step'] == 3) {
                $this->db->where_in('cp.custom_state', array(4));
            } elseif ($param['step'] == 4) {
                $this->db->where_in('cp.custom_state', array(8));
            }
        }

        if ($param['is_down']) {
            $this->db->where('cp.is_down', $param['is_down']);
        }

        if ($param['goods_sn']) {
            $this->db->where("cp.goods_sn like '%{$param['goods_sn']}%' or cp.goods_id = '{$param['goods_sn']}'");
        }

        if ($param['supp_id']) {
            $this->db->where('cp.supp_id', $param['supp_id']);
        }
    }

    public function custom_list($param = array())
    {
        $this->db->from('custom_purchase as cp')
            ->select('cp.*, g.original_img, s.suppliers_name,eo.date_eta,eo.po_id,eo.purchase_json')
            ->join('suppliers as s', 's.suppliers_id = cp.supp_id', 'left')
            ->join('epurchase_orders as eo', 'cp.po_code = eo.po_code', 'left')
            ->join('goods as g', 'cp.goods_id = g.goods_id', 'left');

        $this->_where($param);

        $this->db->order_by('cp.add_time desc');

        $query = $this->db->get();

        $list = $query->result_array();

        $_list = array();
        $po_ids = array();
        foreach ($list as $item) {
            $item['sku'] = json_decode($item['sku'], true);
            $item['sample_img'] = json_decode($item['sample_img'], true);
            $item['purchase_json'] = json_decode($item['purchase_json'], true);

            $po_ids[] = $item['po_id'];

            $_list[$item['custom_id']] = $item;
        }

        $query = $this->db->from('epurchase_goods')->where_in('po_id', $po_ids)->get();
        $_goods = $query->result_array();

        $pur_goods = array();
        if ($_goods) {
            foreach ($_goods as $row) {
                $pur_goods[$row['po_id']][] = $row;
            }
        }


        return array(
            'custom_list' => $_list,
            'pur_goods' => $pur_goods
        );
    }

    public function custom_count($param = array())
    {
        $this->db->from('custom_purchase as cp');

        $this->_where($param);

        return $this->db->count_all_results();
    }

    public function info($custom_id)
    {
        $query = $this->db->from('custom_purchase')->where(array('custom_id' => $custom_id))->get();
        $info = $query->row_array();

        return $info;
    }

    public function insert($data)
    {
        $time = time();

        $_data = array(
            'admin_id' => $_SESSION['admin_id'],
            'goods_id' => $data['goods_id'],
            'sku' => $data['sku'],
            'goods_sn' => $data['goods_sn'],
            'custom_state' => $data['custom_state'],
            'pid' => intval($data['pid']),
            'add_time' => $time
        );

        $this->db->insert('custom_purchase', $_data);

        $custom_id = $this->db->insert_id();

        $data_log = array(
            'custom_id' => $custom_id,
            'custom_state' => 1,
            'remark' => $_data['pid'] ? '重新发起定制' : '发起定制'
        );
        $this->insert_log($data_log);

        return $custom_id;
    }


    //打板添加
    public function update($data, $custom_id)
    {
        $time = time();

        if ($data['custom_state'] == 2) { // 打板
            $_data = array(
                'supp_id' => $data['supp_id'],
                'sample_delivery_time' => strtotime($data['sample_delivery_time']),
                'pattern_price' => $data['pattern_price'],
                'custom_state' => $data['custom_state']
            );

            $_data_log = array(
                'custom_id' => $custom_id,
                'custom_state' => $data['custom_state'],
                'remark' => '打板'
            );
        } elseif ($data['custom_state'] == 3) { //填写运单号
            $_data = array(
                'pattern_on' => $data['pattern_on'],
                'custom_state' => $data['custom_state']
            );

            $_data_log = array(
                'custom_id' => $custom_id,
                'custom_state' => $data['custom_state'],
                'remark' => '填写样品运单号'
            );
        } elseif (in_array($data['custom_state'], array(4,5,6))) { //到样检测

            if ($data['sample_img']) {
                $_data = array(
                    'sample_img' => json_encode($data['sample_img']),
                    'sample_remark' => $data['sample_remark'],
                    'sample_actual_delivery_time' => $time
                );
            }
            $_data['custom_state'] = $data['custom_state'];

            $_data_log = array(
                'custom_id' => $custom_id,
                'custom_state' => $data['custom_state'],
                'remark' => $data['sample_remark']
            );
        } elseif ($data['custom_state'] == 8) { //采购
            $_data = array(
                'po_code' => $data['po_code'],
                'custom_state' => $data['custom_state']
            );

            $_data_log = array(
                'custom_id' => $custom_id,
                'custom_state' => $data['custom_state'],
                'remark' => '采购大货'
            );
        }


        if ($_data) {
            $this->db->where('custom_id', $custom_id);
            $this->db->update('custom_purchase', $_data);
        }

        if ($_data_log) {
            $_data_log['add_time'] = $time;
            $this->insert_log($_data_log);
        }

        return $custom_id;
    }

    public function del($custom_id, $custom_state)
    {
        $this->db->where('custom_id', $custom_id);
        $this->db->update('custom_purchase',array('is_del' => 1));

        $_data_log = array(
            'custom_id' => $custom_id,
            'custom_state' => $custom_state,
            'remark' => $custom_state == 7 ? '已重新打板，废弃' : '样品不合格，废弃',
            'add_time' => time()
        );

        $this->insert_log($_data_log);
    }

    public function insert_log($data)
    {
        $_data = array(
            'custom_id' => $data['custom_id'],
            'admin_id' => $_SESSION['admin_id'],
            'custom_state' => $data['custom_state'],
            'remark' => $data['remark'],
            'add_time' => time()
        );

        $this->db->insert('custom_log', $_data);
    }
}
